package serverlet;

import Server.StudentServer;
import user.Student;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "motifyStudentServlet",urlPatterns = "/motifyStudent")
public class motifyStudentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("UTF-8");
            Student student = new Student();
            StudentServer studentServer = new StudentServer();
            String num = request.getParameter("num");
            student = studentServer.findStudentByIdServer(num);
            System.out.println(student);
            if(!"".equals(request.getParameter("name"))) {
                student.setName(request.getParameter("name"));
            }
            if(!"".equals(request.getParameter("sex"))) {
                student.setSex(request.getParameter("sex"));
            }
            if(!"".equals(request.getParameter("birthday"))) {
                student.setBirthday(request.getParameter("birthday"));
            }
            if(!"".equals(request.getParameter("Email"))) {
                student.setEmail(request.getParameter("Email"));
            }
            if(!"".equals(request.getParameter("remark"))) {
                student.setRemark(request.getParameter("remark"));
            }
            if(!"".equals(request.getParameter("gradeId"))) {
                student.setGradeId(request.getParameter("gradeId"));
                System.out.println(request.getParameter("gradeId"));
            }
            System.out.println(student);
            int i = studentServer.motifyStudentServer(student);
            System.out.println("修改"+i+"行");
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/managerinit");
            requestDispatcher.forward(request,response);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
